page.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. "use client";
  2. import type {ProColumns} from "@ant-design/pro-components";
  3. import {PageContainer, ProCard, ProDescriptions, ProSkeleton, ProTable,} from "@ant-design/pro-components";
  4. import {Button, Divider, Flex} from "antd";
  5. import {fetchApi} from "@/app/_modules/func";
  6. import {useRouter} from "next/navigation";
  7. import {useEffect, useState} from "react";
  8. export default function UserAuth({ params }: { params: { userid: string } }) {
  9. const { push } = useRouter();
  10. //用户信息
  11. const [user, setUser] = useState<any>({});
  12. //角色信息
  13. const [roles, setRoles] = useState([]);
  14. const getUserData = async () => {
  15. const body = await fetchApi(
  16. `/api/system/user/authRole/${params.userid}`,
  17. push
  18. );
  19. if (body !== undefined) {
  20. if (body.code == 200) {
  21. setUser(body.user);
  22. setRoles(body.roles);
  23. setSelectedRowKeys(body.user.roles.map((item: any) => item.roleId));
  24. }
  25. }
  26. };
  27. useEffect(() => {
  28. getUserData();
  29. }, []);
  30. //表格列定义
  31. const columns: ProColumns[] = [
  32. {
  33. title: "序号",
  34. dataIndex: "index",
  35. valueType: "indexBorder",
  36. width: 48,
  37. },
  38. {
  39. title: "角色编号",
  40. dataIndex: "roleId",
  41. search: false,
  42. },
  43. {
  44. title: "角色名称",
  45. dataIndex: "roleName",
  46. search: false,
  47. },
  48. {
  49. title: "权限字符",
  50. dataIndex: "roleKey",
  51. search: false,
  52. },
  53. {
  54. title: "创建时间",
  55. dataIndex: "createTime",
  56. },
  57. ];
  58. //选中行操作
  59. const [selectedRowKeys, setSelectedRowKeys] = useState<React.Key[]>([]);
  60. const rowSelection = {
  61. onChange: (newSelectedRowKeys: React.Key[]) => {
  62. setSelectedRowKeys(newSelectedRowKeys);
  63. },
  64. };
  65. //当前每页条数
  66. const defaultPageSize = 10;
  67. //提交按钮加载状态
  68. const [confirmLoding, setConfirmLoading] = useState(false);
  69. //更新用户角色
  70. const updateAuth = async () => {
  71. setConfirmLoading(true);
  72. const queryParam = {
  73. userId: user.userId,
  74. roleIds: selectedRowKeys.join(","),
  75. };
  76. const body = await fetchApi(
  77. `/api/system/user/authRole?${new URLSearchParams(queryParam)}`,
  78. push,
  79. {
  80. method: "PUT",
  81. }
  82. );
  83. if (body !== undefined) {
  84. if (body.code == 200) {
  85. App.useApp().message.success("授权成功");
  86. } else {
  87. App.useApp().message.error(body.msg);
  88. }
  89. }
  90. setConfirmLoading(false);
  91. };
  92. return (
  93. <PageContainer
  94. header={{
  95. title: "分配角色",
  96. onBack(e) {
  97. push("/system/user");
  98. },
  99. }}
  100. >
  101. {Object.keys(user).length === 0 ? (
  102. <ProSkeleton type="list" />
  103. ) : (
  104. <>
  105. <ProCard title="基本信息">
  106. <ProDescriptions column={24}>
  107. <ProDescriptions.Item span={12} label="用户昵称">
  108. {user.nickName}
  109. </ProDescriptions.Item>
  110. <ProDescriptions.Item span={12} label="用户名称">
  111. {user.userName}
  112. </ProDescriptions.Item>
  113. </ProDescriptions>
  114. </ProCard>
  115. <Divider />
  116. <ProCard title="角色信息">
  117. <ProTable
  118. rowKey="roleId"
  119. rowSelection={{
  120. selectedRowKeys,
  121. ...rowSelection,
  122. }}
  123. tableAlertRender={false}
  124. columns={columns}
  125. dataSource={roles}
  126. pagination={{
  127. defaultPageSize: defaultPageSize,
  128. showQuickJumper: true,
  129. showSizeChanger: true,
  130. // onChange: pageChange,
  131. }}
  132. search={false}
  133. dateFormatter="string"
  134. toolbar={{
  135. actions: [],
  136. settings: [],
  137. }}
  138. />
  139. </ProCard>
  140. <ProCard>
  141. <Flex justify="center" gap="middle">
  142. <Button href="/system/user">返回</Button>
  143. <Button
  144. type="primary"
  145. onClick={updateAuth}
  146. loading={confirmLoding}
  147. >
  148. 提交
  149. </Button>
  150. </Flex>
  151. </ProCard>
  152. </>
  153. )}
  154. </PageContainer>
  155. );
  156. }